//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.gui.fixed;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.table.*;

public class TableViewerS extends JPanel {

    private class MyTableModel extends AbstractTableModel {

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Class getColumnClass(int col) {
            return types[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        public boolean isCellEditable(int row, int col) {
            if (Arrays.binarySearch(listColumnNotEditable, col) >= 0) {
                return false;
            } else {
                return cellEditable;
            }
        }

        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            if (Arrays.binarySearch(listColumnNotEditable, col) < 0) {
                fireTableCellUpdated(row, col);
            }
        }

        MyTableModel() {
        }
    }

    public TableViewerS() {
        jTable = null;
        jScrollPane = null;
        alternateColor = new Color(230, 250, 240);
        cellEditable = false;
        listColumnNotEditable = new int[1];
        types = (new Class[]{
                    java.lang.Integer.class, java.math.BigDecimal.class, java.math.BigDecimal.class
                });
        initialize();
    }

    private void initialize() {
        setLayout(new BorderLayout());
        setSize(306, 140);
        add(getJScrollPane(), "Center");
        clean();
    }

    private JTable getJTable() {
        if (jTable == null) {
            jTable = new JTable();
        }
        return jTable;
    }

    private JScrollPane getJScrollPane() {
        if (jScrollPane == null) {
            jScrollPane = new JScrollPane();
            jScrollPane.setViewportView(getJTable());
        }
        return jScrollPane;
    }

    private void columnSize() {
        if (preferredColumnSize.length != 0) {
            for (int i = 0; i < preferredColumnSize.length; i++) {
                getJTable().getColumnModel().getColumn(i).setPreferredWidth(preferredColumnSize[i]);
            }

        }
    }

    private void clean() {
        cellEditable = false;
        columnNames = (new String[]{
                    " ", " ", " "
                });
        data = new Object[5][3];
        for (int i = 0; i < 5; i++) {
            data[i][0] = null;
            data[i][1] = null;
            data[i][2] = null;
        }

        setData(data);
    }

    public boolean isCellEditable() {
        return cellEditable;
    }

    public void setCellEditable(boolean cellEditable) {
        this.cellEditable = cellEditable;
    }

    public String[] getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(String columnNames[]) {
        this.columnNames = columnNames;
    }

    public Object[][] getData() {
        return data;
    }

    public void setData(Object data[][]) {
        this.data = data;
        getJTable().setModel(new MyTableModel());
        columnSize();
        getJScrollPane().setViewportView(getJTable());
    }

    public void setData(Object data[][], String columnNames[]) {
        this.data = data;
        this.columnNames = columnNames;
        getJTable().setModel(new MyTableModel());
        columnSize();
        getJScrollPane().setViewportView(getJTable());
    }

    public void setData(Object data[][], String columnNames[], int preferredColumnSize[]) {
        this.data = data;
        this.columnNames = columnNames;
        this.preferredColumnSize = preferredColumnSize;
        getJTable().setModel(new MyTableModel());
        columnSize();
        getJScrollPane().setViewportView(getJTable());
    }

    public int[] getPreferredColumnSize() {
        return preferredColumnSize;
    }

    public void setPreferredColumnSize(int preferredColumnSize[]) {
        this.preferredColumnSize = preferredColumnSize;
    }

    public Class[] getTypes() {
        return types;
    }

    public void setTypes(Class types[]) {
        this.types = types;
    }

    public int[] getListColumnNotEditable() {
        return listColumnNotEditable;
    }

    public void setListColumnNotEditable(int listColumnNotEditable[]) {
        this.listColumnNotEditable = listColumnNotEditable;
    }

    public JTable getTable() {
        return getJTable();
    }

    public TableModel getTableModel() {
        return getJTable().getModel();
    }

    public Color getAlternateColor() {
        return alternateColor;
    }

    public void setAlternateColor(Color alternateColor) {
        this.alternateColor = alternateColor;
    }
    private JTable jTable;
    private JScrollPane jScrollPane;
    private Color alternateColor;
    private Object data[][] = {
        new Object[3], new Object[3], new Object[3], new Object[3], new Object[3]
    };
    private String columnNames[] = {
        " ", " ", " "
    };
    private int preferredColumnSize[] = {
        50, 100, 100
    };
    private boolean cellEditable;
    private int listColumnNotEditable[];
    private Class types[];
}
